﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using BZV.Core.Net;
using BZV.Foundation;

namespace BZV.Foundation.Business
{
    public class Query
    {
#region 查询彩票
        /// <summary>
        /// 查询彩票
        /// </summary>
        /// <param name="strID">彩票类型</param>
        /// <param name="strIssue">彩票期号</param>
        public static Lottery QueryLottery(string strID, string strIssue)
        {
            // 离线的时候不查询
            bool bOnLine = NetHelper.CheckOnLine();
            if (!bOnLine)
            {
                return null;
            }

            // 开始查询
            RequestInfo info = new RequestInfo();
            info.URL = LotteryDef.strLotteryQueryAddr;
            info.Accept = @"application/javascript, */*;q=0.8";
            string strParam = "?datatype=json&do=qkjcode&lotID=" + strID +
                                                       "&issue=" + strIssue;
            Lottery lottery = null;
            for (int i = 0; i < 10; i++)                // 查询失败最多10次
            {
                info.URL = LotteryDef.strLotteryQueryAddr;
                string strRes = HttpHelper.GetRequestStr(info, LotteryDef.GetCookie(), strParam);
                lottery = LotteryRequestParser(strRes);
                if (lottery != null)
                {
                    lottery.Create(strID, strIssue, lottery.Code);
                    break;
                }
                System.Threading.Thread.Sleep(700);
            }

            

            return lottery;
        }

        /// <summary>
        /// 解析得到的查询彩票信息
        /// </summary>
        /// <param name="strRes"></param>
        /// <returns></returns>
        public static Lottery LotteryRequestParser(string strRes)
        {
            // 离线的时候不查询
            bool bOnLine = NetHelper.CheckOnLine();
            if (!bOnLine)
            {
                return null;
            }

            // 创建一个彩票
            Lottery lottery = new Lottery();

            // 开始解析
            try
            {
                JArray ja = (JArray)JsonConvert.DeserializeObject(strRes);
                JObject ja1 = (JObject)ja[0];
                string strCode = (string)ja1.GetValue("code");

                // 转换为联系的数值
                string[] strs = strCode.Split(',');
                strCode = "";
                foreach (string str in strs)
                {
                    strCode += str;
                }

                // 设置到彩票
                lottery.Create("", "", strCode);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            return lottery;
        }
#endregion

#region 获取当前时间
        /// <summary>
        /// 获取当前时间
        /// </summary>
        /// <returns></returns>
        public static DateTime QueryCurrentTime()
        {
            // 离线的时候不查询
            bool bOnLine = NetHelper.CheckOnLine();
            if (!bOnLine)
            {
                return DateTime.Now;
            }

            // 开始查询
            RequestInfo info = new RequestInfo();
            info.URL = LotteryDef.strTimeQueryAddr;
            info.Accept = @"application/javascript, */*;q=0.8";
            string strParam = "?do=getcurtime";
            string strRes = HttpHelper.GetRequestStr(info, LotteryDef.GetCookie(), strParam);
            DateTime time = HttpHelper.UnixTimeStampToDateTime(Convert.ToDouble(strRes));
            return time;
        }
#endregion

#region 获取开奖时间
        
        /// <summary>
        /// 获取开奖时间
        /// </summary>
        /// <param name="strID"></param>
        public static StartTime  QueryStartTime(string strID)
        {
            // 离线的时候不查询
            bool bOnLine = NetHelper.CheckOnLine();
            if (!bOnLine)
            {
                return null;
            }

            // 开始查询
            RequestInfo info = new RequestInfo();
            info.URL = LotteryDef.strStartTimeQueryAddr;
            info.Accept = @"application/javascript, */*;q=0.8";
            string strParam = "?LotID=" + strID;
            StartTime time = null;

            for (int i = 0; i < 10; i++)
            {
                info.URL = LotteryDef.strStartTimeQueryAddr;
                string strRes = HttpHelper.GetRequestStr(info, LotteryDef.GetCookie(), strParam);
                time = StartTimeRequestParser(strRes);
                if (time != null)
                {
                    break;
                }
                System.Threading.Thread.Sleep(700);
            }
            
            return time;
        }

        /// <summary>
        /// 解析得到的查询彩票信息
        /// </summary>
        /// <param name="strRes"></param>
        /// <returns></returns>
        public static StartTime StartTimeRequestParser(string strRes)
        {
            // 离线的时候不查询
            bool bOnLine = NetHelper.CheckOnLine();
            if (!bOnLine)
            {
                return null;
            }

            // 创建一个彩票
            StartTime time = new StartTime();

            // 开始解析
            try
            {
                JObject ja1 = (JObject)JsonConvert.DeserializeObject(strRes);
                string strEndTime = (string)ja1.GetValue("EndTime");
                string strFsEndTime = (string)ja1.GetValue("FsEndTime");
                string strFsTimeSpace = (string)ja1.GetValue("FsTimeSpace");
                time.strIssue = (string)ja1.GetValue("Issue");

                // 转换为联系的数值
                time.EndTime = HttpHelper.UnixTimeStampToDateTime(Convert.ToDouble(strEndTime));
                time.FsEndTime = HttpHelper.UnixTimeStampToDateTime(Convert.ToDouble(strFsEndTime));
                time.nWaiteInterval = Convert.ToInt32(strFsTimeSpace);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            return time;
        }
#endregion
        
    }
}
